package com.shzu.jwc.ty.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shzu.jwc.ty.bean.ProblemClass;
import com.shzu.jwc.ty.bean.SelectProblem;
import com.shzu.jwc.ty.common.Msg;
import com.shzu.jwc.ty.service.ProblemClassService;
import com.shzu.jwc.ty.service.SelectProblemservice;
import com.shzu.jwc.ty.service.SystemYearservice;

@Controller
@RequestMapping("selectproblem")
public class SelectProblemcontroller {
	@Autowired
	SelectProblemservice selectproblemservice;
	
	@Autowired
	ProblemClassService problemclassservice;
	@Autowired
	SystemYearservice systemYearservice;
	
	@RequestMapping("/selectproblem.action")
	public String showselectproblem(@RequestParam(value="pn", defaultValue="1") Integer pn,Model model)
	{
		PageHelper.startPage(pn,20);
		List<SelectProblem> selectdata=selectproblemservice.getselectproblem(0);
		List<ProblemClass> classdata=problemclassservice.getproblemclass();
		PageInfo<SelectProblem> pageinfo=new PageInfo<SelectProblem>(selectdata,20);
		model.addAttribute("classdata", classdata);
		model.addAttribute("pageinfo", pageinfo);
		return "/selectproblem/selectproblem";
	}
	
	@ResponseBody
	@RequestMapping("/uploadselect.action")
	public Msg uploadselect(@RequestParam("file") MultipartFile file) throws Exception
	{
		if(file.isEmpty())
			return Msg.error("题库不能为空");
		String name=file.getOriginalFilename();
		if(!name.endsWith(".xlsx")) {
			return Msg.error("文件类型必须为xlsx");
		}
		try {
			selectproblemservice.batchImport(name, file);
			return Msg.success("操作成功");
		}catch(Exception ex) {
			return Msg.error("操作失败,"+ex.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/insertselect.action")
	public Msg insertselect(SelectProblem selectProblem)
	{
		selectProblem.setSelectProblemStatue(0);
		if(selectproblemservice.insertselect(selectProblem)>0)
			return Msg.success("插入成功");
		else
			return Msg.error("插入失败");
	}
	
	@ResponseBody
	@RequestMapping("/deleteselect.action")
	public Msg deleteselect(@RequestParam("id") Integer selectProblemId)
	{
		if(selectproblemservice.deleteselect(selectProblemId)>0)
			return Msg.success("删除成功");
		else
			return Msg.error("删除失败");
	}
	
	@ResponseBody
	@RequestMapping("/updateselectproblem.action")
	public Msg updateselectproblem(@RequestParam("id") Integer id,@RequestParam("column") String column,@RequestParam("value") String value)
	{
		if(selectproblemservice.updateselect(id, column, value)>0)
			return Msg.success("修改成功");
		else
			return Msg.error("修改失败");
	}
	
	@RequestMapping("/searchselect.action")
	public String searchselect(@RequestParam("problemClassId") Integer problemClassId,@RequestParam(value="pn", defaultValue="1") Integer pn,Model model,HttpSession session)
	{
		List<ProblemClass> classdata=problemclassservice.getproblemclass();
		if(problemClassId==0)
		{
			PageHelper.startPage(pn,20);
			List<SelectProblem> selectdata=selectproblemservice.getselectproblem(0);
			PageInfo<SelectProblem> pageinfo=new PageInfo<SelectProblem>(selectdata,20);	
			model.addAttribute("pageinfo", pageinfo);
		}
		else
		{
			PageHelper.startPage(pn,20);
			List<SelectProblem> selectdata=selectproblemservice.getsearchselectproblem(0, problemClassId);
			PageInfo<SelectProblem> pageinfo=new PageInfo<SelectProblem>(selectdata,20);	
			model.addAttribute("pageinfo", pageinfo);
			session.setAttribute("problemClassId", problemClassId);
		}
		model.addAttribute("classdata", classdata);
		return "/selectproblem/selectproblem";
	}
}
